package org.domain;

import java.util.Calendar;
import java.util.Date;

public class Customer {
	private String firstName;	//first name of the customer
	private String lastName;	//last name of the customer
	private String dateOfBirth;	//date of birth of the customer
	private String phoneNumber;		//phone number of the customer
	
	
	//constructor
	public Customer(String firstName, String lastName,String dateOfBirth, String phNumber){
		this.firstName = firstName;
		this.lastName = lastName;
		this.dateOfBirth = dateOfBirth;
		this.phoneNumber = phNumber;
	}
	
	//getters
	public String getFirstName() {
		return firstName;
	}
	
	public String getLastName() {
		return lastName;
	}
	
	public String getDateOfBirth() {
		return dateOfBirth;
	}
	
	public String getPhoneNumber() {
		return phoneNumber;
	}
	
	//customer data validation
	//passes only if first name and last name have only alphabets, date and phone number format is correct and customer is more than 18yrs old
	public boolean isCustomerDataValid(){
		if(!firstName.matches("^[a-zA-Z]+$")){
			System.out.println("Error. First Name can have only alphabets");
			return false;
		}
			
		if(!lastName.matches("^[a-zA-Z]+$"))
		{
			System.out.println("Error. Last Name can have only alphabets");
			return false;
		}
		if(!dateOfBirth.matches("^([1-9]|0[1-9]|1[0-2])[/]([1-9]|0[1-9]|1[0-9]|2[0-9]|3[0-1])[/](1[9][0-9][0-9]|2[0][0-9][0-9])$")){
			System.out.println("Error. Date of birth needs to be in mm/dd/yyyy format");
			return false;
		}
		if(isCustomerLessThan18Years()){
			System.out.println("Error. To book a flight, customer needs to be 18 years or older");
			return false;
		}
		if(!phoneNumber.matches("^\\d{3}-\\d{3}-\\d{4}$")){
			System.out.println("Error. Phone number should follow xxx-xxx-xxxx format");
			return false;
		}
		return true;
	}
	
	
	//checks if customer is more than 18 years old
	@SuppressWarnings("deprecation")
	private boolean isCustomerLessThan18Years() {
		Calendar dob = Calendar.getInstance();
		dob.setTime(new Date(dateOfBirth));
		Calendar today = Calendar.getInstance();
		int age = today.get(Calendar.YEAR) - dob.get(Calendar.YEAR);
		if (today.get(Calendar.DAY_OF_YEAR) < dob.get(Calendar.DAY_OF_YEAR))
		age--;
		if(age < 18)
			return true;
		else {
			return false;
		}
	}
}
